package updown.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import updown.constant.GeneralConstant;
import updown.constant.LogConstant;
import updown.controller.other.BaseController;
import updown.domain.*;
import updown.service.updown.DownUpScheduleService;
import updown.util.RandomUtil;

import java.util.Map;
import java.util.concurrent.ScheduledFuture;

/**
 * <p>Title: updown.controller </p>
 * <p>Description:updown: - 定时重启过i </p>
 *
 * @Author:jiangkaixin
 * @Date:2022/1/5
 * @Version: 1.0
 */
@Slf4j
@Controller
@RequestMapping("/schedule")
public class ScheduleController extends BaseController {

    @Autowired
    private DownUpScheduleService downUpScheduleService;

    /**
     * 上传jar 保存到本地，封装一个定时器任务
     * @param file
     * @return
     */
    @PostMapping("/scheduleTask")
    @ResponseBody
    public String scheduleTask(@RequestBody ServiceMapVo vo) {
        if(StringUtils.isEmpty(vo.getScheduleTime())){
            log.info("没有指定时间");
            return "没有配置时间";
        }
        String taskId = RandomUtil.getRandomStr();
        vo.scheduleTask();
        log.info("uploadJarAndScheduleTask(),vo:{}", vo);
        // 2 上传 jar包 到本地
//        String localFilePath = localFileService.downToLocal(file, vo.getOriginalFileName(), vo.getServiceName(), vo.getJarVersionInfo(), taskId);
//        log.info("直接保存,:{}", localFilePath);
        // 3. 创建 serviceInfo 信息
        ServiceInfo serviceInfo = ServiceInfo.builder().serviceName(vo.getServiceName()).build();
        // 4 创建 task
        Jar jar = Jar.builder()
                .version(vo.getJarVersionInfo())
                .originalFileName(vo.getOriginalFileName())
                .localFileLocation(vo.getLocalFilePath()).build();

        DownUpSchedule downUpSchedule = DownUpSchedule.builder()
                .serviceName(vo.getServiceName())
                .scheduleTime(vo.getScheduleTime())
                .jarVersionInfo(vo.getJarVersionInfo())
                .originalFileName(vo.getOriginalFileName())
                .localFilePath(vo.getLocalFilePath()).build();

        TaskWrapper taskWrapper = TaskWrapper.builder().jar(jar)
                .downUpSchedule(downUpSchedule)
                .taskId(taskId)
                .taskType(GeneralConstant.TASK_WRAPPER_DOWN_UP_SCHEDULE)
                .serviceInfo(serviceInfo)
                .build();
        downUpScheduleService.downUpSchedule(taskWrapper);

        log.info("提交任务，返回定时任务信息:{}", taskWrapper);
        addLog("创建服务定时替换任务：" + vo.getServiceName(), LogConstant.MODULE_SCHEDULE);
        return "服务定时替换成功";
    }

    /**
     * 取消定时任务
     * @param file
     * @return
     */
    @GetMapping("/cancelDownUpSchedule")
    @ResponseBody
    public String cancelDownUpSchedule(@RequestParam String taskId) {
        if (StringUtils.isEmpty(taskId)){
            return "必须选择取消的任务id";
        }
        TaskWrapper taskWrapper = DownUpScheduleService.taskMap.get(taskId);
        log.info("log.info:taskId >>>:{}",taskId);
        downUpScheduleService.cancelDownUpSchedule(taskId);
        log.info("取消定时任务:{}", taskId);
        addLog("取消定时任务：" + (taskWrapper != null ? taskWrapper.getServiceName() : null), LogConstant.MODULE_SCHEDULE);
        return "success";
    }

    @GetMapping("/taskMap")
    @ResponseBody
    public Map<String, TaskWrapper> taskMap() {
        return DownUpScheduleService.taskMap;
    }

    @GetMapping("/futureMap")
    @ResponseBody
    public Map<String, ScheduledFuture> futureMap() {
        return DownUpScheduleService.futureMap;
    }
}
